package module.building.view.buildingUpPanel
{
	import code.utils.ButtonUtil;
	
	import com.darwin.darwinUI.containers.Box;
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.LabelButton;
	import com.darwin.darwinUI.controls.Tab;
	import com.darwin.darwinUI.controls.TabViewStack;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.UIManager;
	import com.darwin.darwinUI.uiconst.UIAlign;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.Dictionary;
	
	import main.App;
	
	import module.building.event.BuildEvent;
	import module.building.model.dataObj.BuildingObj;
	import module.main.view.ResourceConst;
	
	import mx.resources.ResourceBundle;
	
	import org.osmf.events.BufferEvent;
	
	/**
	 * 建筑升级面板
	 * @author killsoon
	 * 
	 */	
	public class BuildingUpTabView extends UIComponent
	{
		private var bg:UIComponent;
		private var btTitle:LabelButton;
		private var btUp:LabelButton;
		private var btCancel:LabelButton;
		private var btClose:Button;
		public var buildingUpTabPanel:BuildingUpTabPanel;
		private var $dataDic:Dictionary = new Dictionary();
		
		private var _buildingObj:BuildingObj;
		
		
		public function BuildingUpTabView(buildingObj:BuildingObj = null)
		{
			super();
			
			//			if(buildingObj == null)
			//			{
			//				throw new Error("建筑升级面板参数出错！");
			//			}
			_buildingObj = buildingObj;
			
			initView();
			initListener();
		}
		
		public function checkBuild():String
		{
			if(buildingUpTabPanel && !buildingUpTabPanel.canUp){
				
				ButtonUtil.setButtonStyle(ButtonUtil.BUTTON_STYLE_NORMAL,btUp,btUp.background);
			
				return "资源不足";
			}else
			{
//				btUp = ButtonUtil.createGeneralButton(TextFieldFactory.getInstance().getLabel("buildingLevelUp_button_levelUp"));
//				btUp.x = 145.7;
//				btUp.y = 383.1;
//				addChild(btUp);
				return "";
			}
		}
		
		private function initView():void
		{
			createBg();
			createTabPanel();
			createButton();
		}
		
		private function initListener():void
		{
			addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);
			btUp.addEventListener(MouseEvent.CLICK, buildingUp);
			btCancel.addEventListener(MouseEvent.CLICK, cancelUp);
			btClose.addEventListener(MouseEvent.CLICK, close);
		}
		
		private function removeFromStage(e:Event):void
		{
			removeAllChild();
		}
		
		private function buildingUp(e:MouseEvent):void
		{
			dispatchEvent(new BuildEvent(BuildEvent.BUILD_UPGRADE, null));
		}
		
		private function cancelUp(e:MouseEvent):void
		{
			dispatchEvent(new BuildEvent(BuildEvent.BUILD_UP_CLOSE, null));
		}
		
		private function close(e:MouseEvent = null):void
		{
			dispatchEvent(new BuildEvent(BuildEvent.BUILD_UP_CLOSE, null));
		}
		
		private function createBg():void
		{
			bg = new UIComponent();
			bg.backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_BG;
			bg.backgroundAuto = true;
			bg.background.width = 480.6;
			bg.background.height = 430;
			addChild(bg);
			this.width = bg.background.width;
			this.height = bg.background.height;
			
			btTitle = new LabelButton();
			btTitle.label = TextFieldFactory.getInstance().getLabel("buildingLevelUp_title");
			btTitle.backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_TITLE;
			btTitle.backgroundAuto = true;
			btTitle.text = UIManager.getLanguageManager().getText("buildingLevelUp_title", [UIManager.getLanguageManager().getText(_buildingObj.name)]);
			//			btTitle.text = "兵营升级";
			btTitle.textX = (btTitle.background.width - btTitle.textWidth) / 2 - 3;
			btTitle.textY = 11;
			btTitle.x = (bg.background.width - btTitle.background.width) / 2;
			btTitle.y = -15;
			addChild(btTitle);
			btTitle.mouseChildren = false;
			btTitle.mouseEnabled = false;
		}
		
		public function select():void
		{
			$tempTab.selected = true;
		}
		
		private function createButton():void
		{
			btUp = ButtonUtil.createGeneralButton(TextFieldFactory.getInstance().getLabel("buildingLevelUp_button_levelUp"),90,32,ButtonUtil.BUTTON_STYLE_BLUE);
			//			btUp.x = bg.background.width / 2 - 14 - btUp.background.width;
			//			btUp.y = bg.background.height - 65;
			btUp.x = 145.7;
			btUp.y = 383.1;
			addChild(btUp);
			
			btCancel = ButtonUtil.createGeneralButton(TextFieldFactory.getInstance().getLabel("button_cancel"));
			//			btCancel.x = bg.background.width / 2 + 14;
			//			btCancel.y = bg.background.height - 65;
			btCancel.x = 253.7;
			btCancel.y = 383.1;
			addChild(btCancel);
			
			btClose = new Button();
			btClose.backgroundSkin = ResourceConst.BUILD_BUTTON_CLOSE;
			btClose.backgroundAuto = true;
			btClose.x = bg.background.width - 40;
			btClose.y = 12;
			addChild(btClose);
		}
		
		private	var $tempTab:Tab;
		private function createTabPanel():void
		{
			var $tabPanelContainer:UIComponent = new UIComponent();
			var $tabViewStack:TabViewStack = new TabViewStack($tabPanelContainer);
			var $tab:UpgradeTab;
			var $tabBox:UIComponent = new UIComponent();
			var arrow:UIComponent;
			
			buildingUpTabPanel = new BuildingUpTabPanel();
			
			var tempBuildingObj:BuildingObj = _buildingObj.clone();//这里直接传原等级就OK
			buildingUpTabPanel.data = tempBuildingObj;
			var vectorTabSelected:Vector.<Sprite> = _buildingObj.getLevelIconArray(52);
			
			for(var i:int = 0; i < vectorTabSelected.length; i++)
			{		
				var buildingSelectedSprite:Sprite = vectorTabSelected[i];				
				$tab = new UpgradeTab(buildingSelectedSprite,i)
				$tab.x = 73 * i;
				$tabBox.addChild($tab);
				
				var object:Object = {};
				object.wood = _buildingObj.levelInfo[i].wood;
				object.gold = _buildingObj.levelInfo[i].gold;
				object.iron = _buildingObj.levelInfo[i].iron;
				object.stone = _buildingObj.levelInfo[i].stone;
				$dataDic[$tab] = object;
				
				$tabViewStack.addBinding(buildingUpTabPanel, $tab);
				
				if(_buildingObj.level == i+1)
				{
					$tempTab = $tab;
					arrow  = new UIComponent();
					arrow.backgroundSkin = ResourceConst.UPGRADE_ARROW;
					arrow.backgroundAuto = true;
					arrow.x = $tempTab.x + 52;
					arrow.y = $tempTab.y + 15;
				}
			}
			if(arrow){
				$tabBox.addChild(arrow);
			}
			if($tempTab == null)
			{
				$tempTab = $tab;
			}
			buildingUpTabPanel.dataDic = $dataDic;
			$tabPanelContainer.x = 16;
			$tabPanelContainer.y = 88;
			addChild($tabPanelContainer);
			$tabBox.x = 27;
			$tabBox.y = 37;
			addChild($tabBox);
//			$tempTab.selected = true;
		}
	}
}